<template>
  <div class="member-order-page">
    <XtxTabs v-model="activeName" @tab-click="clickTab">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
      ></XtxTabsPanel>
    </XtxTabs>

    <!-- 订单列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
        @on-logistics="handlerLogistics"
        @on-delete="handlerDelete"
        @on-cancel="handlerCancel"
        @on-confirm="handlerConfirm"
        :order="item"
        v-for="item in orderList"
        :key="item.id"
      ></OrderItem>
    </div>

    <!-- 分页 -->
    <XtxPagination
      v-if="total > reqParams.pageSize"
      @current-page="reqParams.page = $event"
      :total="total"
      :page-size="reqParams.pageSize"
      :current-page="reqParams.page"
    />

    <OrderLogistics ref="logisticsOrderCom" />
    <OrderCancel ref="orderCancelCom" />
  </div>

  <!-- 对于像：对话框，消息提示，确认提示，通知组件 适合使用传送门 Teleport -->
</template>
<script>
import { orderStatus } from '@/api/constants'
import OrderItem from './components/order-item.vue'
import { reactive, ref, watch } from 'vue'
import { findOrderList, delteOrder, confirmOrder } from '@/api/order'
import OrderCancel from './components/order-cancel.vue'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
import OrderLogistics from './components/order-logistics'
export default {
  name: 'MemberOrder',
  components: { OrderItem, OrderCancel, OrderLogistics },
  setup () {
    const activeName = ref('all')
    // 获取数据
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      orderState: 0
    })
    const clickTab = ({ index }) => {
      reqParams.page = 1
      reqParams.orderState = index
    }
    // 监听reqParams数据变化，初次调用
    const loading = ref(false)
    const total = ref(0)
    const getOrderListFn = () => {
      loading.value = true
      findOrderList(reqParams).then((data) => {
        orderList.value = data.result.items
        loading.value = false
        total.value = data.result.counts
      })
    }
    watch(
      reqParams,
      () => {
        getOrderListFn()
      },
      { immediate: true }
    )

    const orderList = ref([])
    // 删除订单
    const handlerDelete = (order) => {
      console.log(order)
      Confirm({ text: '亲，你确定删除该订单吗？' })
        .then((data) => {
          delteOrder([order.id]).then(() => {
            Message({ text: '删除订单成功', type: 'success' })
            getOrderListFn()
          })
        })
        .catch((e) => {})
    }

    return {
      activeName,
      total,
      clickTab,
      orderStatus,
      orderList,
      reqParams,
      loading,
      handlerDelete,
      ...useCancel(),
      ...useConfirm(),
      ...useLogistics()
    }
  }
}
// 取消订单逻辑
export const useCancel = () => {
  // 组件实例
  const orderCancelCom = ref(null)
  const handlerCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}
// 封装逻辑-确认收货
export const useConfirm = () => {
  const handlerConfirm = (item) => {
    // item 就是你要确认收货的订单
    Confirm({ text: '您确认收到货吗？确认后货款将会打给卖家。' }).then(() => {
      confirmOrder(item.id).then(() => {
        Message({ text: '确认收货成功', type: 'success' })
        // 确认收货后状态变成 待评价
        item.orderState = 4
        getOrderListFn()
      })
    })
  }
  return { handlerConfirm }
}
// 查看物流
export const useLogistics = () => {
  const logisticsOrderCom = ref(null)
  const handlerLogistics = (item) => {
    logisticsOrderCom.value.open(item)
  }
  return { handlerLogistics, logisticsOrderCom }
}
</script>
<style scoped lang="less">
.order-list {
  background: #fff;
  padding: 20px;
}
.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;
  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;
    span {
      margin-right: 20px;
      &.down-time {
        margin-right: 0;
        float: right;
        i {
          vertical-align: middle;
          margin-right: 3px;
        }
        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }
    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }
  .body {
    display: flex;
    align-items: stretch;
    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;
      > p {
        padding-top: 10px;
      }
      &:first-child {
        border-left: none;
      }
      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;
        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;
            &:last-child {
              border-bottom: none;
            }
            .image {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }
            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;
              p {
                margin-bottom: 5px;
                &.name {
                  height: 38px;
                }
                &.attr {
                  color: #999;
                  font-size: 12px;
                  span {
                    margin-right: 5px;
                  }
                }
              }
            }
            .price {
              width: 100px;
            }
            .count {
              width: 80px;
            }
          }
        }
      }
      &.state {
        width: 120px;
        .green {
          color: @xtxColor;
        }
      }
      &.amount {
        width: 200px;
        .red {
          color: @priceColor;
        }
      }
      &.action {
        width: 140px;
        a {
          display: block;
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}

.order-list {
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/load.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
